package com.feng.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

public class HttpTools {
	
	
	/**
	 * get
	 * 
	 * @param strURL
	 * @return
	 */
	public static String httpGet(String strURL) {
		String re = "";
		HttpURLConnection urlConn = null;
		InputStream in = null;
		List<String> list = new ArrayList<String>();
		try {
			URL url = new URL(strURL);
			urlConn = (HttpURLConnection) url.openConnection();
			urlConn.setDoOutput(true);
			urlConn.setConnectTimeout(1000);
			urlConn.setRequestMethod("GET");

			in = urlConn.getInputStream();
			BufferedReader rd = new BufferedReader(new InputStreamReader(in,
					"GBK"));
			String line = rd.readLine();
			while (line != null) {
				line = line.trim();
				if (!line.equals(""))
					list.add(line);
				line = rd.readLine();
			}

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (in != null)
				try {
					in.close();
				} catch (IOException e) {
				}

			if (urlConn != null) {
				urlConn.disconnect();
			}
		}

		if (list != null && list.size() > 0) {
			for (int i = 0; i < list.size(); i++) {
				re += list.get(i) + "\n";
			}
		}

		return re.trim();
	}
	
	
	public static String httpPost(String host, String data) {
		String result = "";
		try {
			URL url = new URL(host);
			HttpURLConnection connection = (HttpURLConnection) url
					.openConnection();
			connection.setDoOutput(true);
			connection.setDoInput(true);
			connection.setReadTimeout(5000000);
			connection.setRequestMethod("POST");
			// connection.setRequestProperty("Content-type", "text");
			// connection.setRequestProperty("Content-Type","application/xml;charset=UTF-8");
			connection.setRequestProperty("Content-Type",
					"application/x-www-form-urlencoded;charset=UTF-8");
			connection
					.setRequestProperty(
							"user-Agent",
							"Mozilla/4.0 (compatible; MSIE 6.0; MS Web Services Client Protocol 2.0.50727.4223)");
			// SEND DNS INFO
			OutputStreamWriter out = new OutputStreamWriter(
					connection.getOutputStream(), "UTF-8");
			out.write(data);// SEND DATA
			out.flush();
			out.close();
			// GET RESPONSE
			InputStream in = connection.getInputStream();
			BufferedReader rd = new BufferedReader(new InputStreamReader(in,
					"UTF-8"));

			String line = rd.readLine();
			List<String> list = new ArrayList<String>();
			while (line != null) {
				line = line.trim();
				if (!line.equals(""))
					list.add(line);
				line = rd.readLine();
			}
			if (list != null) {
				if (list.size() == 1) {
					result += list.get(0);
				} else if (list.size() > 1) {
					for (int i = 0; i < list.size(); i++) {
						result += list.get(i) + "\n";
					}
				}
			}
		} catch (MalformedURLException e) {

			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {

			e.printStackTrace();
		} catch (IOException e) {

			e.printStackTrace();
		}

		return result;
	}


}
